package dao;

import entity.Address;
import entity.User;
import util.AddressUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddressDao extends BaseDao {
    public AddressDao() {
        super();
    }

    /**
     * 保存地址信息
     */
    public void save(Address address) {
        String sql = "INSERT INTO t_shipping(user_id, receiver_name, receiver_phone, receiver_mobile, receiver_state, receiver_city, receiver_district, receiver_address, receiver_zip, is_default, status, created, updated) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())";
        // 拿到刚刚插入的数据的add_id
        int add_id = this.updateData(sql, address.getUserId(), address.getReceiverName(), address.getReceiverPhone(), address.getReceiverMobile(), address.getReceiverState(), address.getReceiverCity(), address.getReceiverDistrict(), address.getReceiverAddress(), address.getReceiverZip(), address.getIsDefault(), address.getStatus());
        address.setAddId(add_id);
    }

    /**
     * 根据add_id修改全部地址信息
     */
    public void updateAll(Address address) {
        String sql = "UPDATE t_shipping SET receiver_name = ?, receiver_phone = ?, receiver_mobile = ?, receiver_state = ?, receiver_city = ?, receiver_district = ?, receiver_address = ?, receiver_zip = ?, is_default = ?, status = ?, updated = NOW() WHERE add_id = ?";
        this.updateData(sql, address.getReceiverName(), address.getReceiverPhone(), address.getReceiverMobile(), address.getReceiverState(), address.getReceiverCity(), address.getReceiverDistrict(), address.getReceiverAddress(), address.getReceiverZip(), address.getIsDefault(), address.getStatus(), address.getAddId());
    }

    /**
     * 根据add_id修改座机号码
     */
    public void updatePhone(Address address) {
        String sql = "UPDATE t_shipping SET receiver_phone = ?, updated = NOW() WHERE add_id = ?";
        this.updateData(sql, address.getReceiverPhone(), address.getAddId());
    }

    /**
     * 根据add_id修改手机号码
     */
    public void updateMobile(Address address) {
        String sql = "UPDATE t_shipping SET receiver_mobile = ?, updated = NOW() WHERE add_id = ?";
        this.updateData(sql, address.getReceiverMobile(), address.getAddId());
    }

    /**
     * 根据add_id修改收货人姓名
     */
    public void updateName(Address address) {
        String sql = "UPDATE t_shipping SET receiver_name = ?, updated = NOW() WHERE add_id = ?";
        this.updateData(sql, address.getReceiverName(), address.getAddId());
    }

    /**
     * 根据add_id修改收货信息中的省份、城市、区县、详细地址
     */
    public void updateAddress(Address address) {
        String sql = "UPDATE t_shipping SET receiver_state = ?, receiver_city = ?, receiver_district = ?, receiver_address = ?, updated = NOW() WHERE add_id = ?";
        this.updateData(sql, address.getReceiverState(), address.getReceiverCity(), address.getReceiverDistrict(), address.getReceiverAddress(), address.getAddId());
    }

    /**
     * 根据add_id修改邮政编码
     */
    public void updateZip(Address address) {
        String sql = "UPDATE t_shipping SET receiver_zip = ?, updated = NOW() WHERE add_id = ?";
        this.updateData(sql, address.getReceiverZip(), address.getAddId());
    }

    /**
     * 根据add_id修改是否为默认地址 （是否为默认地址，1-true,0-false）
     */
    public void updateDefault(Address address) {
        String sql = "UPDATE t_shipping SET is_default = ?, updated = NOW() WHERE add_id = ?";
        this.updateData(sql, address.getIsDefault(), address.getAddId());
    }

    /**
     * 根据add_id修改地址状态（地址状态，1-正常，2-删除）
     */
    public void updateStatus(Address address) {
        String sql = "UPDATE t_shipping SET status = ?, updated = NOW() WHERE add_id = ?";
        this.updateData(sql, address.getStatus(), address.getAddId());
    }

    /**
     * 根据add_id删除地址信息 （这个是真的删除）
     */
    public void delete(Address address) {
        String sql = "DELETE FROM t_shipping WHERE add_id = ?";
        this.updateData(sql, address.getAddId());
    }

    /**
     * 根据user_id查询地址信息
     */
    public List<Address> findByUserId(int user_id) {
        List<Address> list = null;
        String sql = "SELECT * FROM t_shipping WHERE user_id = ? AND status = 1";
        try (ResultSet rs = this.queryData(sql, user_id)) {
            if (rs == null) return null;
            list = new ArrayList<>();
            while (rs.next()) {
                list.add(AddressUtil.rsToAddress(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 通过User对象获取user_id查询地址信息
     */
    public List<Address> findByUserId(User user) {
        return this.findByUserId(user.getId());
    }

    /**
     * 返回全部地址信息
     */
    public List<Address> findAll() {
        List<Address> list = null;
        String sql = "SELECT * FROM t_shipping WHERE status = 1";
        try (ResultSet rs = this.queryData(sql)) {
            if (rs == null) return null;
            list = new ArrayList<>();
            while (rs.next()) {
                list.add(AddressUtil.rsToAddress(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
